/*
 * @Descripttion:
 * @Author: voanit
 * @Date: 2022-08-23 10:52:34
 * @LastEditors: voanit
 * @LastEditTime: 2022-08-27 17:10:15
 */
import Vue from 'vue'
import App from './App'

import uView from 'uview-ui'
Vue.use(uView)

import store from './store'

/* 
配置uni.request的封装
*/
uni.$u.http.setConfig((config) => {
  /* config 为默认全局配置*/
  config.baseURL = `https://api-hmugo-web.itheima.net/api/public/v1` /* 根域名 */
  return config
})

// request 是请求拦截器
uni.$u.http.interceptors.request.use(
  (config) => {
    // 1. 获取 token
    const { token } = store.state.user
    // 2. 判断有 token 并且路径以 /my/ 开头
    if (token && config.url.startsWith('/my/')) {
      // 3. 在请求头中携带 token 信息
      config.header = {
        ...config.header,
        Authorization: token,
      }
    }
    return config
  },
  (config) => {
    return Promise.reject(config)
  }
)
// response 是响应拦截器
uni.$u.http.interceptors.response.use(
  (response) => {
    return response
  },
  (response) => {
    return Promise.reject(response)
  }
)

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
  store,
  ...App,
})
app.$mount()
